package com.xxl.job.executor.test;

import com.xxl.job.executor.crawler.XxlCrawler;
import com.xxl.job.executor.crawler.annotation.PageFieldSelect;
import com.xxl.job.executor.crawler.annotation.PageSelect;
import com.xxl.job.executor.crawler.conf.XxlCrawlerConf;
import com.xxl.job.executor.crawler.parser.PageParser;
import lombok.Data;
import lombok.Value;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.Test;

import javax.persistence.Id;
import java.security.acl.LastOwnerException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 描述:
 * test
 *
 * @author Pfly
 * @create 2019-01-07 9:54
 */
public class test3 {
    @PageSelect(cssQuery = "body")

    @Data
    public static class PageVo {

        /*商品sku*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field:nth-child(1) span.label", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> label;

        /*商品sku*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field:nth-child(2) span.label", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> label2;

        /*商品sku*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field:nth-child(3) span.label", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> label3;

        /*商品sku*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field:nth-child(4) span.label", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> label4;

        /*商品sku*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field:nth-child(5) span.label", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> label5;

        /*商品sku*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field:nth-child(6) span.label", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> label6;

        /*商品sku*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field:nth-child(1) span.value", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> value;

        /*商品sku*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field:nth-child(2) span.value", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> value2;

        /*商品sku*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field:nth-child(3) span.value", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> value3;

        /*商品sku*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field:nth-child(4) span.value", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> value4;

        /*商品sku*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field:nth-child(5) span.value", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> value5;

        /*商品sku*/
        @PageFieldSelect(cssQuery = "#condition-all div.row div.seller-field div.field:nth-child(6) span.value", selectType = XxlCrawlerConf.SelectType.HTML)
        private List<String> value6;
    }
    @Test
    public void  crawler (){

        Map headerMap = new HashMap();
        Map cookieMap = new HashMap();

        cookieMap.put("BUYER_CITY_SELECTED", "80");

        headerMap.put("Cache-Control", "max-age=0");

        String url = "https://uae.souq.com/ae-en/apple-iphone-xs-max-with-facetime-512gb-4g-lte-gold-38545201/io/";
        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls(url)
                .setThreadCount(1).setPauseMillis(1000)
                .setAllowSpread(false)
                .setCookieMap(cookieMap)
                .setHeaderMap(headerMap)
                .setPageParser(new PageParser<PageVo>() {
                    @Override
                    public boolean parse(Document html, Element pageVoElement, PageVo pageVo){
                        List<String> item_id = new ArrayList<>();




                        try {
                            if(getItemid(pageVo.getValue(),pageVo.getLabel()) != null){
                                item_id = getItemid(pageVo.getValue(),pageVo.getLabel());
                            } else if (getItemid(pageVo.getValue2(),pageVo.getLabel2()) != null){
                                item_id = getItemid(pageVo.getValue2(),pageVo.getLabel2());
                            } else if (getItemid(pageVo.getValue3(),pageVo.getLabel3()) != null){
                                item_id = getItemid(pageVo.getValue3(),pageVo.getLabel3());
                            } else if (getItemid(pageVo.getValue4(),pageVo.getLabel4()) != null){
                                item_id = getItemid(pageVo.getValue4(),pageVo.getLabel4());
                            } else if (getItemid(pageVo.getValue5(),pageVo.getLabel5()) != null){
                                item_id = getItemid(pageVo.getValue5(),pageVo.getLabel5());
                            } else if (getItemid(pageVo.getValue6(),pageVo.getLabel6()) != null){
                                item_id = getItemid(pageVo.getValue6(),pageVo.getLabel6());
                            }


                            for (String s : item_id) {
                                System.out.println(s);
                            }


                        }catch (Exception e){
                            e.printStackTrace();
                        }

                        return false;
                    }
                })
                .build();
        crawler.start(true);
    }

    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }

    public List<String> getItemid (List<String> value, List<String> lable){
                    try {

                        if(lable==null){
                            return null;
                        }
                        boolean item_id = lable.get(1).contains("Item ID");

                        if(item_id){
                            return value;
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
        return null;
    }
}
